<?php
/*qq登陆api
 * 
 *
 * oid会在server缓存20分钟，期间不会重复调用oid的api，从而提高访问效率
 * 
 * 根据oid获取对应的用户信息
 * 
 * 授权之后会自动加入用户到数据库，不需要注册，从而简化用户的操作
 * 
 */

//正常调用时才加载核心数据
if(empty($_GET))
die('err');

require dirname(dirname(dirname(__FILE__)))."/share/share.php";
require_once(ROOT."module/qq/API/qqConnectAPI.php");

$test=false;

$qc = new QC(true);#初始化qc

$access_token=$qc->at;
$openid=$qc->oid;



//获取用户信息
$info = $qc->get_user_info();

//测试
if($test){  
	
	
echo sprintf('at:%s<br>openid:%s<br>ses:%s',$access_token,$openid,$qc->recorder->read("access_token"));	
echo($openid);
echo '<meta charset="UTF-8">';
echo "<p>";
echo "gender:".$info["gender"];
echo "</p>";
echo "<p>";
echo "nickname:".$info["nickname"];
echo "</p>";
echo "<p>";
echo "figureurl:<img src=\"".$info['figureurl']."\">";
echo "<p>";
echo "<p>";
echo "figureurl_2:<img src=\"".$info['figureurl_2']."\">";
echo "<p>";
echo "<p>";
echo "figureurl_qq_2:<img src=\"".$info['figureurl_qq_2']."\">";
echo "<p>";
echo "vip:".$info["vip"];
echo "</p>";
echo "level:".$info["level"];
echo "</p>";
echo "is_yellow_year_vip:".$info["is_yellow_year_vip"];
echo "</p>";

die();	
}


$dt['nickname']=$info['nickname'];
$dt['sex']=$info['gender'];
$dt['QQ_pic']=$info['figureurl_qq_2'];
$dt['Qzone_pic']=$info['figureurl_2'];
$dt['addr']=sprintf('%s,%s',$info['province'],$info['city']);
$dt['years']=$info['year'];
$dt['openid']=$openid;



//openid获取成功
if(!empty($openid)){
	   	
setSession($m->data["qq"],$openid);
//查询user表，已存在的用户会更新信息，不存在的会新建
$output = show("user",$dt['openid'],'openid');

//新增
if($output==null){
$dt['createtime']=getTime(TIMESTAMP);
$dt['lastlogintime']=getTime(TIMESTAMP);
$dt['IP']=C_IP;
update('user',$dt,0,'js:window.close()');  
} 	
//修改
else{   		
$id=$output['Id'];   
$dt['lastlogintime']=getTime(TIMESTAMP);	
$dt['IP']=C_IP;	
update('user',$dt,$id,'js:window.close()');  
}

   	 	
}

 
show_json(1,'ok');